package com.baymax.test.scene

import android.app.ActivityManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Messenger
import android.util.Log
import android.view.LayoutInflater
import android.view.PointerIcon
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.airbnb.mvrx.asMavericksArgs
import com.airbnb.mvrx.withState
import com.baymax.lib.logger.KLog
import com.baymax.test.scene.demo.AnimatorScene
import com.baymax.test.scene.demo.AudioRecorderScene
import com.baymax.test.scene.demo.ChainScene
import com.baymax.test.scene.demo.CheckBoxScene
import com.baymax.test.scene.demo.CircleScene
import com.baymax.test.scene.demo.ConstrainRemoveTestScene
import com.baymax.test.scene.demo.DNSDemo
import com.baymax.test.scene.demo.DevInfoScene
import com.baymax.test.scene.demo.DownloadScene
import com.baymax.test.scene.demo.DrawOrderScene
import com.baymax.test.scene.demo.DrawableScene
import com.baymax.test.scene.demo.FocusScene
import com.baymax.test.scene.demo.FragmentScene
import com.baymax.test.scene.demo.FrescoScene
import com.baymax.test.scene.demo.GoneSurfaceScene
import com.baymax.test.scene.demo.GroupScrollScene
import com.baymax.test.scene.demo.GuavaScene
import com.baymax.test.scene.demo.HSViewScene
import com.baymax.test.scene.demo.HttpScene
import com.baymax.test.scene.demo.KeyCombineScene
import com.baymax.test.scene.demo.KeyScene
import com.baymax.test.scene.demo.KillSecondScene
import com.baymax.test.scene.demo.LNAnimatorScene
import com.baymax.test.scene.demo.LaunchModeScene
import com.baymax.test.scene.demo.LayerTypeTest
import com.baymax.test.scene.demo.LayoutManagerScene
import com.baymax.test.scene.demo.LayoutScene
import com.baymax.test.scene.demo.MarkDownDemo
import com.baymax.test.scene.demo.MaverickDemo
import com.baymax.test.scene.demo.MediaDaoScene
import com.baymax.test.scene.demo.MouseScene
import com.baymax.test.scene.demo.OpenGLScene
import com.baymax.test.scene.demo.OpenIQYScene
import com.baymax.test.scene.demo.PicDemo
import com.baymax.test.scene.demo.PlayerCaseScene
import com.baymax.test.scene.demo.PluginTestScene
import com.baymax.test.scene.demo.*
import com.baymax.test.scene.demo.RvLayerScene
import com.baymax.test.scene.demo.SeekBarScene
import com.baymax.test.scene.demo.SpanScene
import com.baymax.test.scene.demo.SurfaceScene
import com.baymax.test.scene.demo.TextDrawableScene
import com.baymax.test.scene.demo.TextHeightScene
import com.baymax.test.scene.demo.TextShaderScene
import com.baymax.test.scene.demo.VRFocusScene
import com.baymax.test.scene.demo.WKAnimatorScene
import com.baymax.test.scene.demo.maverick.ResolveIntentViewModel
import com.baymax.test.scene.demo.maverick.sceneViewModel
import com.baymax.test.scene.deo.TestSeekBarScene
import com.baymax.test.scene.internal.dump
import com.baymax.test.scene.life.SecondActivity
import com.bytedance.scene.ktx.requireNavigationScene
import com.cfzx.library.scene.BaseWebScene
import kotlinx.coroutines.launch


class ProjectTestScene : BaseGroupScene() {

    private val viewmodel: ResolveIntentViewModel by sceneViewModel()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            requireActivity().window.decorView.pointerIcon =
                PointerIcon.load(resources, R.drawable.pointer_res_default_icon)
        }
        lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                Log.d(
                    "ProjectTestScene",
                    "LifecycleEventObserver onStateChanged $source :: $event "
                )
            }
        })
        viewmodel.onEach(ResolveIntentViewModel.ResolveIntent::url, deliveryMode = uniqueOnly()) {
            println("url change $it")
            if (!it.isNullOrBlank()) {
                requireNavigationScene().push(PlayerTestScene().apply {
                    setArguments(it.asMavericksArgs())
                })
            }
        }

        viewmodel.onEach(
            ResolveIntentViewModel.ResolveIntent::webUrl,
            deliveryMode = uniqueOnly()
        ) {
            println("weburl change $it")
            if (!it.isNullOrBlank()) {
                requireNavigationScene().push(BaseWebScene().apply {
                    setArguments(it.asMavericksArgs())
                })
            }
        }

        viewmodel.onEach(
            ResolveIntentViewModel.ResolveIntent::route,
            deliveryMode = uniqueOnly()
        ) {
            println("route to $it")
            if (!it.isNullOrBlank()) {
                when (it) {
                    "demo/animator" -> requireNavigationScene().push(LNAnimatorScene())
                    else -> Unit
                }
            }
        }

    }

    override fun invalidate() {
        super.invalidate()
        withState(viewmodel) {
            println("invalidate state $it")
        }

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup,
        savedInstanceState: Bundle?
    ) = container.newFlexContainer(attachParent = false) {
        this.id = R.id.container
        addButton("test cons") {
            requireNavigationScene().push(ConstrainRemoveTestScene())
        }

        addButton("go second") {
            requireSceneContext().startActivity(
                Intent(
                    requireSceneContext(),
                    SecondActivity::class.java
                )
            )

        }
        addButton("go address") {
            requireSceneContext().applicationContext.startActivity(Intent().apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                setComponent(
                    ComponentName(
                        "com.baymax.sdk.address",
                        "com.baymax.sdk.address.MainActivity"
                    )
                )
            })

        }
        addButton("open gl") {
            requireNavigationScene().push(OpenGLScene())
        }
        addButton("dev info") {
            requireNavigationScene().push(DevInfoScene())
        }
        if (Build.VERSION.SDK_INT <= 19) {
            addButton("vr focus") {
                requireNavigationScene().push(VRFocusScene())
            }
        }
        addButton("plugin test") {
            requireNavigationScene().push(PluginTestScene())
        }
        addButton("open IQY") {
            requireNavigationScene().push(OpenIQYScene())
        }
        addButton("open player container gone demo") {
            requireNavigationScene().push(GoneSurfaceScene())
        }
        addButton("open player test seek") {
            requireNavigationScene().push(PlayerCaseScene())
        }

        addButton("add circle") {
            requireNavigationScene().push(CircleScene())
        }
        addButton("add edit") {
            requireNavigationScene().push(FocusScene())
        }
        addButton("add horscroll") {
            requireNavigationScene().push(GroupScrollScene())
        }

        addButton("fresco") {
            requireNavigationScene().push(FrescoScene())
        }
        addButton("img") {
            requireNavigationScene().push(KeyScene())
        }
        addButton("http") {
            requireNavigationScene().push(HttpScene())
        }
//            addButton("test provider") {
//                requireNavigationScene().push(ProviderTest())
//            }
//            addButton("test rxjava") {
//                requireNavigationScene().push(RxjavaScene())
//            }
//            addButton("open browser") {
//                requireNavigationScene().push(OpenBrowserTest())
//            }
//            addButton("open TextInputScene") {
//                requireNavigationScene().push(TextInputScene())
//            }
//            addButton("open mediaplayer") {
//                requireNavigationScene().push(MediaPlayerScene())
//            }
        addButton("LayerTypeTest") {
            requireNavigationScene().push(LayerTypeTest())
        }
        addButton("open  drawable") {
            requireNavigationScene().push(DrawableScene())
        }
        addButton("open  rv layer") {
            requireNavigationScene().push(RvLayerScene())
        }
        addButton("test Fragment") {
            requireNavigationScene().push(FragmentScene())
        }
        addButton("test txt size,height") {
            requireNavigationScene().push(TextHeightScene())
        }
        addButton("TextDrawableScene") {
            requireNavigationScene().push(TextDrawableScene())
        }

        addButton("chain") {
            requireNavigationScene().push(ChainScene())
        }
        addButton("launch mode") {
            requireNavigationScene().push(LaunchModeScene())
        }

        addButton("text shader") {
            requireNavigationScene().push(TextShaderScene())
        }
        addButton("hs view") {
            requireNavigationScene().push(HSViewScene())
        }
//            addButton("open  cons demo") {
//                requireNavigationScene().push(ConstrainRemoveTestScene())
//            }
//            addButton("open  activity callback") {
//                requireNavigationScene().push(ActivityCallBackScene())
//            }
//
            addButton("test notification") {
                requireNavigationScene().push(NotificationScene())
            }
//
//            addButton("share test") {
//                requireNavigationScene().push(ShareScene())
//            }
        addButton("draw order") {
            requireNavigationScene().push(DrawOrderScene())
        }
//            addButton("animator 21") {
//                requireNavigationScene().push(Type21AnimatorScene())
//            }
        addButton("animator") {
            requireNavigationScene().push(AnimatorScene())
        }
//            addButton("resource") {
//                requireNavigationScene().push(ResourceScene())
//            }
//            addButton("canvas") {
//                requireNavigationScene().push(CanvasScene())
//            }
//            addButton("replace demo") {
//                requireNavigationScene().push(ReplaceScene())
//            }
//            addButton("memory") {
//                requireNavigationScene().push(MemoryScene())
//            }
//
        addButton("jumptest") {
            requireNavigationScene().push(JumpScene())
        }
//            addButton("ViewPagerScene") {
//                requireNavigationScene().push(ViewPagerScene())
//            }


//            addButton("test cons") {
//                Log.d(TAG, "start")
//                val p = DanmakuPlayer(SimpleRenderer(), null)
//                Log.d(TAG, "p $p ");
//            }
        addButton("play rtsp") {
            requireNavigationScene().push(RTSPScene())
        }

        addButton("span test") {
            requireNavigationScene().push(SpanScene())
        }
        addButton("GuavaScene test") {
            requireNavigationScene().push(GuavaScene())
        }
        addButton("layout manager") {
            requireNavigationScene().push(LayoutManagerScene())
        }
        addButton("media dao") {
            requireNavigationScene().push(MediaDaoScene())
        }
        addButton("check box") {
            requireNavigationScene().push(CheckBoxScene())
        }

        addButton("key combine") {
            requireNavigationScene().push(KeyCombineScene())
        }
        addButton("surface with layer") {
            requireNavigationScene().push(SurfaceScene())
        }

        addButton("player test") {
            requireNavigationScene().push(PlayerTestScene())
        }
        addButton("pic demo list") {
            requireNavigationScene().push(PicDemo())
        }
        addButton("mavericjs") {
            requireNavigationScene().push(MaverickDemo())
        }
        addButton("layout") {
            requireNavigationScene().push(LayoutScene())
        }

        addButton("dns") {
            requireNavigationScene().push(DNSDemo())
        }

        addButton("down load and play") {
            requireNavigationScene().push(DownloadScene())
        }

        addButton("mouse pos") {
            requireNavigationScene().push(MouseScene())
        }

        addButton("test wk animator") {
            requireNavigationScene().push(WKAnimatorScene())
        }
        addButton("seek bar") {
            requireNavigationScene().push(SeekBarScene())
        }
        addButton("test ln animator") {
            requireNavigationScene().push(LNAnimatorScene())
        }
        addButton("test audio") {
            requireNavigationScene().push(AudioRecorderScene())
        }
        addButton("launch second") {
            val intent =
                Intent(container.context.applicationContext, SecondActivity::class.java).apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

                }
            requireActivity().startActivity(intent)
        }
        addButton("test seek bar") {
            requireNavigationScene().push(TestSeekBarScene())

        }
        addButton("kill process") {
            requireNavigationScene().push(KillSecondScene())
        }
        addButton("test markdown"){
            requireNavigationScene().push(MarkDownDemo())
        }
    }


    override fun onPause() {
        super.onPause()
        requireActivity().dump()
    }
}